﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SharePoint;

namespace RaisingStudio.Data.Common.Builders
{    
    public class SharePointCommandBuilder : CommandBuilder
    {
        public SharePointCommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, System.Type type)
            : base(connection, databaseType, providerName, type)
        {

        }

        public SharePointCommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, System.Type type, string name)
            : base(connection, databaseType, providerName, type, name)
        {

        }

        private string GetViewFields(System.Data.Common.DataTableMapping dataTableMapping)
        {
            // TODO: ViewFields, another mapping not the same one, column name does not match.
            /*
            if ((dataTableMapping != null) && (dataTableMapping.ColumnMappings != null) && (dataTableMapping.ColumnMappings.Count > 0))
            {
                StringBuilder viewFieldsStringBuilder = new StringBuilder();
                viewFieldsStringBuilder.Append("<ViewFields>");
                foreach (System.Data.Common.DataColumnMapping dataColumnMapping in dataTableMapping.ColumnMappings)
                {
                    viewFieldsStringBuilder.Append(GetViewFieldString(dataColumnMapping));
                }
                viewFieldsStringBuilder.Append("</ViewFields>");
                return viewFieldsStringBuilder.ToString();
            }
            */
            return null;
        }

        private string GetViewFieldString(System.Data.Common.DataColumnMapping dataColumnMapping)
        {
            return string.Format("<FieldRef Name='{0}'/>", dataColumnMapping.SourceColumn);
        }

        public override System.Data.IDbCommand GetSelectCommand(out System.Data.Common.DataTableMapping dataTableMapping)
        {
            SharePointCommand command = base.GetSelectCommand(out dataTableMapping) as SharePointCommand;
            command.CommandText = string.Empty;
            command.ListUrl = dataTableMapping.SourceTable;
            command.ViewFields = GetViewFields(dataTableMapping);
            return command;
        }

        public override System.Data.IDbCommand GetSelectCommand(CommonCommand conditionCommand, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            System.Data.IDbCommand command = GetSelectCommand(out dataTableMapping);
            if (conditionCommand != null)
            {
                command.CommandText = conditionCommand.CommandText;
            }
            return command;
        }

        public override System.Data.IDbCommand GetSelectCommand(string[] columnNames, CommonCommand conditionCommand, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            // TODO: 
            System.Data.IDbCommand command = GetSelectCommand(columnNames, out dataTableMapping);
            command.CommandText = conditionCommand.CommandText;
            return command;
        }

        public override System.Data.IDbCommand GetSelectCommand(string[] columnNames, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            // TODO: 
            SharePointCommand command = base.GetSelectCommand(columnNames, out dataTableMapping) as SharePointCommand;
            command.CommandText = string.Empty;
            command.ListUrl = dataTableMapping.SourceTable;
            command.ViewFields = GetViewFields(dataTableMapping);
            return command;
        }
   }
}
